﻿using System;
using System.Text;
using System.Web.Caching;
using System.Diagnostics;

namespace CKS.EBE.Caching
{
	public class SPFileCacheDependency : CacheDependency
	{
		private string _uniqueID = Guid.NewGuid().ToString();
		private string _siteUrl;
		private string _itemUrl;

		public void RaiseChangedEvent(object sender, SPFileCacheEventArgs e)
		{
			NotifyDependencyChanged(sender, e);
		}

		public SPFileCacheDependency(string siteUrl, string fileUrl)
		{
			_itemUrl = fileUrl.ToLower();
			_siteUrl = siteUrl.ToLower();
			SPFileWatcher.Current(siteUrl).AddDepedentItem(this, fileUrl);
		}

		public override string GetUniqueID()
		{
			return _uniqueID;
		}

		protected override void DependencyDispose()
		{
			try
			{
				SPFileWatcher.Current(_siteUrl).RemoveDepedentItem(this, _itemUrl);

				base.DependencyDispose();
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex);
			}
		}
	}
}